package com.example.wan1;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import com.example.wan1.base.BaseMvpActivity;
import com.example.wan1.base.InjectPresenter;
import com.example.wan1.data.ResponseList;
import com.example.wan1.login.LoginContract;
import com.example.wan1.login.LoginPresenter;
import com.example.wan1.login.PersonBean;
import com.example.wan1.look.LookContract;
import com.example.wan1.look.LookPresenter;
import com.example.wan1.model.ArticleBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseMvpActivity implements LoginContract.View, LookContract.View {

    @BindView(R.id.et1)
    EditText mEt1;
    @BindView(R.id.et2)
    EditText mEt2;
    @BindView(R.id.tv1)
    TextView mTv1;
    @BindView(R.id.tv2)
    TextView mTv2;
    private PersonBean mPersonBean;

    private static final String TAG = "MainActivity";

    @InjectPresenter
    private LoginPresenter mPresenter;

    @InjectPresenter
    private LookPresenter mLookPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mPersonBean = new PersonBean();


        PersonBean.DataBean data = new PersonBean.DataBean();

        data.setUsername(mEt1.getText().toString());
        data.setPassword(mEt2.getText().toString());
        mPersonBean.setData(data);
    }

    @Override
    public PersonBean getUserInfo() {
        return mPersonBean;
    }

    @Override
    public void loginSuccess(PersonBean user) {
        Log.e(TAG, "loginSuccess: " + user.getErrorCode());
    }

    @Override
    public void loginFailure(String msg) {

    }

    @OnClick({R.id.tv1, R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv1:
//                presenter.login();
                mPresenter.login();
                break;
            case R.id.tv2:
                mLookPresenter.getData();
                break;
            case R.id.tv3:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
        }
    }

//    @Override
//    public void onSuccess(LookBean bean) {
//        Log.e(TAG, "onSuccess: 当前页" + bean.getData().getCurPage());
//    }

    @Override
    public void onSuccess(ResponseList<ArticleBean> bean) {
        Log.e(TAG, "onSuccess: 当前页 数组长度" + bean.getDatas().size());

        int total = bean.getTotal();
        int currentPage = bean.getCurPage();
        int pageCount = bean.getPageCount();
        List<ArticleBean> datas = bean.getDatas();

        Log.i(TAG, "total: " + total);
        Log.i(TAG, "currentPage: " + currentPage);
        Log.i(TAG, "pageCount: " + pageCount);
        for (int i = 0; i < datas.size(); i++) {
            Log.i(TAG, "onSuccess: " + datas.get(i).getTitle());
        }

    }

    @Override
    public void onFailure(String msg) {

    }
}
